using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Admin_Links : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ddl_Cats.ClearSelection();
        }
    }


    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 1;
        Link linCurrent = LinkManager.GetLink(Convert.ToInt32(GridView1.SelectedValue));
        tbo_Url.Text = linCurrent.URL;
        tbo_Display.Text = linCurrent.Display;
        tbo_Alt.Text = linCurrent.Alt;
        tbo_Discription.Text = linCurrent.Description;

        hfi_ID.Value = linCurrent.ID.ToString(); 


        //GridViewRow GVR = GridView1.SelectedRow;      
        
        //tbo_Url.Text = GVR.Cells[2].Text;
        //tbo_Display.Text = GVR.Cells[1].Text;
        //tbo_Alt.Text = GVR.Cells[3].Text;       
        //tbo_Discription.Text = GVR.Cells[4].Text;

        //hfi_ID.Value = GVR.Cells[7].Text; 

        ddl_Cats.ClearSelection();
        try
        {
            ddl_Cats.Items.FindByValue(linCurrent.CategorID.ToString()).Selected = true;
        }
        catch
        {
        }

    }
    protected void but_NewCat_Click(object sender, EventArgs e)
    {
        Response.Redirect("linkcategories.aspx");
    }
    protected void but_Save_Click(object sender, EventArgs e)
    {
        Link linCurrent = new Link();

        if (GridView1.SelectedIndex > -1)
        {

            linCurrent.Alt = tbo_Alt.Text;
            linCurrent.CategorID = Convert.ToInt32(ddl_Cats.SelectedValue);
            linCurrent.Description = tbo_Discription.Text;
            linCurrent.Display = tbo_Display.Text;
            linCurrent.ID = Convert.ToInt32(hfi_ID.Value);
            linCurrent.URL = tbo_Url.Text;

            LinkManager.UpdateLink(linCurrent);
            
            

        }
        else
        {
            linCurrent.Alt = tbo_Alt.Text;
            linCurrent.CategorID = Convert.ToInt32(ddl_Cats.SelectedValue);
            linCurrent.Description = tbo_Discription.Text;
            linCurrent.Display = tbo_Display.Text;           
            linCurrent.URL = tbo_Url.Text;

            LinkManager.InsertLink(linCurrent);
            
        }

        clearFields();
        MultiView1.ActiveViewIndex = 0;
        GridView1.DataBind();
    }
    protected void but_New_Click(object sender, EventArgs e)
    {
        clearFields();
        MultiView1.ActiveViewIndex = 1;
    }

    private void clearFields()
    {
        GridView1.SelectedIndex = -1;
        tbo_Display.Text = "";
        tbo_Discription.Text = "";
        tbo_Alt.Text = "";
        tbo_Url.Text = "";
        hfi_ID.Value = "";
        hfi_Del_ID.Value = "";

        ddl_Cats.ClearSelection();
    }
    

    
    
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
   
        hfi_Del_ID.Value = e.Keys[0].ToString();
    }
    protected void SqlDataSource1_Deleted(object sender, SqlDataSourceStatusEventArgs e)
    {
        clearFields();
        //MultiView1.ActiveViewIndex = 0;
        GridView1.DataBind();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //on edit this is no delete button and this will chuck an exception if we try and get the button.
            if (e.Row.Cells[8].Controls.Count > 0)
            {
                LinkButton lbuDelete = (LinkButton)e.Row.Cells[8].Controls[0];
                lbuDelete.OnClientClick = "return confirm(\'Are you sure you want to delete? This can not be undone!\');";
            }// end of the check to see if there are any controls in the cell

            //// the following code finds the delete button cell 
            //for (int intCounter = 0; intCounter < e.Row.Cells.Count; intCounter++)
            //{
            //    try
            //    {
            //        LinkButton lbuDelete = (LinkButton)e.Row.Cells[intCounter].Controls[0];

            //        if (lbuDelete.Text.ToLower().StartsWith("d"))
            //        {
            //            Response.Redirect(intCounter.ToString());
            //            //lbuDelete.OnClientClick = "return confirm(\'Are you sure you want to delete? This can not be undone.\');";
            //        }
            //    }
            //    catch
            //    {
            //    }
            //}    
        }// end of the if 
    }
    protected void butCancel_Click(object sender, EventArgs e)
    {
        clearFields();
        MultiView1.ActiveViewIndex = 0;
    }
}
